<template>
        <div class="p20" style="width:350px">
            <ui-title>修改密码</ui-title>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="旧密码" prop="oldPwd" :rules="{ validator: pwdRule, trigger: 'blur' }">
                    <el-input v-model="form.oldPwd" placeholder="请输入旧密码"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPwd" :rules="{ validator: pwdRule, trigger: 'blur' }">
                    <el-input v-model="form.newPwd" placeholder="请输入新密码"></el-input>
                </el-form-item>
                <el-form-item label="重复新密码" prop="newPwdRecheck" :rules="{ validator: pwdRule, trigger: 'blur' }">
                    <el-input v-model="form.newPwdRecheck" placeholder="重复新密码"></el-input>
                </el-form-item>
            </el-form>
            <div class="flex row-end">
                <el-button @click="changePwd" type="primary" class="ui-button-large">确定修改</el-button>
            </div>
        </div>
</template>
<script>
    export default {
        data(){
            return {
                form:{
                    oldPwd:'',
                    newPwd:'',
                    newPwdRecheck:'',
                }
            }
        },
        methods:{
            changePwd(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$http.post('index.php?g=home&m=Users&a=change_password',{
                            old:this.form.oldPwd,
                            new:this.form.newPwd
                        })
                        .then(({data})=>{
                            console.log('修改密码', data)
                            if (data.code===1) {
                                this.$router.replace('/user-all')
                            }
                        })
                    }
                })
            },
            pwdRule(rule, value, callback){
                console.log(rule);
                if (!value) {
                    callback(new Error('不能为空'))
                    return 
                }
                if (rule.field==='newPwd') {
                    if (this.form.oldPwd===this.form.newPwd) {
                        callback(new Error('新密码不能与旧密码一致'))
                        return 
                    }
                }
                if (rule.field==='newPwdRecheck') {
                    if (this.form.newPwd!==this.form.newPwdRecheck) {
                        callback(new Error('两次输入新密码不一致'))
                        return
                    }
                }
                callback()
            },
        },
    }
</script>